package com.hyt.it.ogt.controller.ykcj.exam;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hyt.it.ogt.controller.base.BaseController;
import com.hyt.it.ogt.ykcj.common.annotation.Log;
import com.hyt.it.ogt.ykcj.common.core.domain.AjaxResult;
import com.hyt.it.ogt.ykcj.common.core.page.TableDataInfo;
import com.hyt.it.ogt.ykcj.common.enums.BusinessType;
import com.hyt.it.ogt.ykcj.common.enums.ExamFormEnums;
import com.hyt.it.ogt.ykcj.common.enums.ExamTypeEnums;
import com.hyt.it.ogt.ykcj.common.utils.poi.ExcelUtil;
import com.hyt.it.ogt.ykcj.domain.exam.ExamInfo;
import com.hyt.it.ogt.ykcj.domain.exam.ExamNameConfig;
import com.hyt.it.ogt.ykcj.domain.exam.ExamSubjectInfo;
import com.hyt.it.ogt.ykcj.domain.param.ExamineeCheckParam;
import com.hyt.it.ogt.ykcj.domain.vo.ExamConfigNameVo;
import com.hyt.it.ogt.ykcj.domain.vo.ExamSubjectVo;
import com.hyt.it.ogt.ykcj.service.exam.IExamInfoService;
import com.hyt.it.ogt.ykcj.service.exam.IExamNameConfigService;
import com.hyt.it.ogt.ykcj.service.exam.IExamSubjectInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiSort;

/**
 * 考试科目信息Controller
 * 
 * @author admin
 * @date 2021-06-18
 */
@Api(tags = "exam.7.考试科目信息", value = "考试科目信息请求接口")
@ApiSort(value = 107)
@RestController
@RequestMapping("/exam/subjectInfo")
public class ExamSubjectInfoController extends BaseController
{
    @Autowired
    private IExamSubjectInfoService examSubjectInfoService;

    @Autowired
    private IExamInfoService examInfoService;

    @Autowired
    private IExamNameConfigService examNameConfigService;

    /**
     * 查询考试科目信息列表
     */
    @ApiOperation("查询考试科目信息列表")
    @GetMapping("/list")
    public TableDataInfo list(ExamSubjectInfo examSubjectInfo)
    {
        startPage();
        List<ExamSubjectInfo> list = examSubjectInfoService.selectExamSubjectInfoList(examSubjectInfo);
        return getDataTable(list);
    }

    /**
     * 导出考试科目信息列表
     */
    @ApiOperation("导出考试科目信息列表")
    @Log(title = "考试科目信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ExamSubjectInfo examSubjectInfo)
    {
        List<ExamSubjectInfo> list = examSubjectInfoService.selectExamSubjectInfoList(examSubjectInfo);
        ExcelUtil<ExamSubjectInfo> util = new ExcelUtil<ExamSubjectInfo>(ExamSubjectInfo.class);
        return util.exportExcel(list, "考试科目信息数据");
    }

    /**
     * 获取考试科目信息详细信息
     */
    @ApiOperation("获取考试科目信息详细信息")
    @ApiImplicitParam(name = "examId", value = "考试ID", dataType = "String", required = true)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(examSubjectInfoService.selectExamSubjectInfoById(id));
    }


    @ApiOperation("获取子考试or科目选择项")
    @GetMapping("/getExamSubjectList/{examId}/{examForm}")
    public AjaxResult getExamList(@PathVariable String examId,@PathVariable Integer examForm) {
        //考试组
        if(ExamFormEnums.FORM_EXAM_GROUP.getCode().equals(examForm)){
            // 查询子考试
            ExamInfo param = new ExamInfo();
            param.setRelationId(examId);
            List<ExamInfo> examInfos = examInfoService.selectExamInfoList(param);
            if (CollectionUtils.isEmpty(examInfos)) {
                return AjaxResult.success(Collections.EMPTY_LIST);
            }
            List<ExamConfigNameVo> list = new ArrayList<>();
            for (ExamInfo examInfo : examInfos) {
                ExamConfigNameVo examConfigNameVo = new ExamConfigNameVo();
                ExamNameConfig examNameConfig = examNameConfigService.selectExamNameConfigById(examInfo.getExamNameConfigId());
                examConfigNameVo.setName(examNameConfig.getName());
                examConfigNameVo.setExamNameId(examInfo.getId());
                examConfigNameVo.setId(examInfo.getId());
                list.add(examConfigNameVo);
            }
            // 返回的考试日期列表
            return AjaxResult.success(list);
        } else {
            // 查询年份对应的考试类别
            List<ExamSubjectVo> list = examSubjectInfoService.selectExamSubjectInfoByExamId(examId);
            // 返回的考试日期列表
            return AjaxResult.success(list);
        }
    }

    /**
     * 查询考试科目信息列表
     */
    @ApiOperation("查询考试科目信息列表")
    @ApiImplicitParam(name = "examId", value = "考试ID", dataType = "String", required = true)
    @GetMapping("/getExamSubjectInfoList/{examId}")
    public AjaxResult getExamSubjectInfoList(@PathVariable String examId) {
        return AjaxResult.success(examSubjectInfoService.getExamSubjectInfoList(examId));
    }


    /**
     * 获取最新的考试的科目
     */
    @ApiOperation("获取最新的考试的科目")
    @GetMapping("/getExamSubjectListRecent")
    public AjaxResult getExamList() {
        // 目前直接返回企业招聘考试的数据
        List<ExamSubjectVo> list = examSubjectInfoService.selectExamSubjectInfoByExamType(ExamTypeEnums.ENTERPRISE.getCode());
        return AjaxResult.success(list);
    }


    @ApiImplicitParam(name = "examId", value = "考试ID", dataType = "String", required = true)
    @GetMapping("/getExamSubjectList/{examId}")
    public AjaxResult getExamList(@PathVariable String examId)
    {
        // 查询年份对应的考试类别
        List<ExamSubjectVo> list = examSubjectInfoService.selectExamSubjectInfoByExamId(examId);
        // 返回的考试日期列表
        return AjaxResult.success(list);
    }

    @ApiImplicitParam(name = "subjectIds", value = "科目id", dataType = "String", required = true)
    @PostMapping("/examineeRepeatCheck")
    public AjaxResult examineeRepeatCheck(@RequestBody ExamineeCheckParam examineeCheckParam){
        // 校验科目里面是否存在
        examSubjectInfoService.examineeRepeatCheck(examineeCheckParam.getSubjectIds());
        return AjaxResult.success();
    }

}
